﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using FieldTemplate;

namespace WpfDevelopHelper
{
    /// <summary>
    /// RandomNumSet.xaml 的交互逻辑
    /// </summary>
    public partial class RandomNumSet : UserControl
    {
        public RandomNumSet()
        {
            InitializeComponent();
        }
    }

    [Serializable]
    public class RandomNumFieldSetVm : FieldSetVmBase
    {

        [NonSerialized]
        Random dm;
        public RandomNumFieldSetVm()
        {
            FieldName = "身高";
            FieldType = "randomnum";
            SetPanel = new RandomNumSet();
            SetPanel.DataContext = this;


            dm = new Random();
            min = 1;
            max = 999999;
        }



        private int _min;
        public int min
        {
            get
            {
                return _min;
            }
            set
            {
                _min = value;
                RaisePropertyChanged("min");
            }
        }
        private int _max;
        public int max
        {
            get
            {
                return _max;
            }
            set
            {
                _max = value;
                RaisePropertyChanged("max");
            }
        }

        private int _DecimalDigits;
        public int DecimalDigits
        {
            get
            {
                return _DecimalDigits;
            }
            set
            {
                _DecimalDigits = value;
                RaisePropertyChanged("DecimalDigits");
            }
        }
        private bool _IsDouble;
        public bool IsDouble
        {
            get
            {
                return _IsDouble;
            }
            set
            {
                _IsDouble = value;
                RaisePropertyChanged("IsDouble");
            }
        }
        public override Fieldinfo Serialize()
        {
            var tmp = base.Serialize();
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "min", PropertyType = "int", PropertyValue = min.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "max", PropertyType = "int", PropertyValue = max.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "IsDouble", PropertyType = "bool",PropertyValue = IsDouble.ToString() });
            tmp.propertyinfos.Add(new Propertyinfo() { PorpertyName = "DecimalDigits", PropertyType = "int", PropertyValue = DecimalDigits.ToString() });

            return tmp;
        }
        public override string GetFieldValue()
        {
            if(IsDouble)
            {
                //即：返回的值范围包括 minValue 但不包括 maxValue
                int tmp = dm.Next(min , max-1 );
                double tmp2 = dm.NextDouble();
                //大于或等于 0.0 且小于 1.0 的双精度浮点数。
                return Math.Round(tmp+tmp2, DecimalDigits).ToString();
            }
            return dm.Next(min,max).ToString();
        }
    }

}
